<?php

namespace Item;

use C\L\AdmController;
use C\M\User;

class AprlistController extends AdmController
{
    protected function init()
    {
        $this->service = $this->s_itemaprlist;

        $this->pubSearchKeys = [
        'cid'
        ];

        $this->hideKeys = [
        'is_delete'
        ];

        $this->timeToDateKeys = [
        'addtime', 'uptime', 'ok_time', 'back_time'
        ];
    }

    protected function befortSearch()
    {
        $page_num = $this->getValue('page_num', false, 'int');
        if (!$page_num) {
            $this->params['page_num'] = 20;
        }
        $this->params['order'] = 'back_time desc';

        return true;
    }

    protected function afterSearch(&$data)
    {
        if ($data['page_curren'] == 1 and !empty($data['list'])) {
            $money_info = $this->s_itemaprlist->moneyBackList($data['list'][0]['cid']);

            if ($money_info) {
                array_unshift($data['list'], $money_info);
            }
        }
        foreach ($data['list'] as &$item) {
            if (!isset($item['type'])) {
                $item['type'] = 2;
            }
            $item['back_time_date'] = ($item['back_time']) ? date('Y/m/d H:i:s', $item['back_time']) : '';
            $item['ok_time_date'] = ($item['ok_time']) ? date('Y/m/d H:i:s', $item['ok_time']) : '';
        }

        return true;
    }

    public function getAprListAction()
    {
        $currentPage = $this->request->getQuery('page', 'int', 1);
        $perPage     = $this->request->getQuery('per_page', 'int', 20);
        $userMobile  = $this->request->getQuery('user', 'string', null);
        $start_date  = $this->request->getQuery('start_date', 'string', null);
        $end_date    = $this->request->getQuery('end_date', 'string', null);
        $type        = $this->request->getQuery('type', 'string', null);
        $status      = $this->request->getQuery('status', 'string', null);

        $params = [];

        $userId = null;
        if ($userMobile) {
            $user = User::findFirst("mobile = '$userMobile'");
            $userId = $user->uid ?? PHP_INT_MAX;

            $params['user_id'] = $userId;
        }

        if ($start_date && $end_date) {
            $params['start_date'] = $start_date;
            $params['end_date'] = $end_date;
        }

        if ($type) {
            $params['type'] = $type;
        }
        
        if ($status) {
            $params['status'] = $status;
        }

        $result = $this->s_itemaprlist->getAprListAction($currentPage, $perPage, $params);

        return $this->success($result);
    }

    public function dispatchNowAction()
    {
        $cids = $this->request->getPost('cids');

        return $this->s_itemaprlist->dispatchByCids($cids)
          ? $this->success()
          : $this->error();
    }

    public function dispatchAllAction()
    {
        return $this->s_itemaprlist->dispatchAll()
          ? $this->success()
          : $this->error();
    }
}
